package com.example.demo1;

/**
 * 循环打印
 */
public class LoopPrint {

    public static  final String LOCK = "lock";
    public static  Boolean printNum = true;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                int index = 1;
                while (true){
                    synchronized (LOCK){
                        if(printNum){
                            System.out.print((index++)%9);
                            printNum = false;
                        }
                        LOCK.notify();
                        try {
                            LOCK.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                int index = 65;
                while (true){
                    synchronized (LOCK){
                        if(!printNum){
                            System.out.print((char)((index++)));
                            printNum = true;
                        }
                        LOCK.notify();
                        try {
                            LOCK.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }


                }
            }
        }).start();
    }
}
